package server;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.border.EtchedBorder;
import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.UIManager;
import java.awt.SystemColor;

/**
 * @author Idan
 *Server GUI
 */
public class ServerPanel extends JPanel
{
	private JLabel addressLabel;
	private JButton closeServerButton;
	private JTextField host;
	private JTextField port;
	private JTextField database;
	private JTextField username;
	private JTextField password;
	private JButton runServerButton;
	private ServerPanel me;
	private JButton btnNewButton;
	
	
	
	public ServerPanel()
	{
		setBackground(new Color(173, 216, 230));
		this.me = this;
		setLayout(new MigLayout("", "[grow]", "[grow]"));
		
		JPanel panel = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		panel.setBackground(new Color(173, 216, 230));
		panel.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "\u05E9\u05E8\u05EA \u05D7.\u05E0.\u05D9.\u05EA", TitledBorder.CENTER, TitledBorder.TOP, null, new Color(0, 0, 0)));
		add(panel, "cell 0 0,grow");
		panel.setLayout(new MigLayout("", "[grow][][grow]", "[grow][][][][grow]"));
		
		JPanel panel_1 = new JPanel(){@Override
			public void paintComponent(Graphics g) {
	        Graphics2D g2d = (Graphics2D) g;
	        Color color1 = getBackground();
	        Color color2 = color1.darker();
	        int w = getWidth();
	        int h = getHeight(); 
	        GradientPaint gp = new GradientPaint(
	                0, 0, color1,
	                0, h, color2);

	        g2d.setPaint(gp);
	        g2d.fillRect(0, 0, w, h);
	        }};
		panel_1.setBackground(new Color(173, 216, 230));
		panel_1.setBorder(new TitledBorder(null, "", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(59, 59, 59)));
		panel.add(panel_1, "cell 1 1,grow");
		panel_1.setLayout(new MigLayout("", "[grow][grow][][grow]", "[16px][][][][][]"));
		
		addressLabel = new JLabel("טוען..");
		panel_1.add(addressLabel, "cell 1 0,alignx right");
		
		JLabel label = new JLabel("כתובת לחיבור:");
		panel_1.add(label, "cell 2 0,alignx center,aligny top");
		
		host = new JTextField();
		host.setBackground(Color.LIGHT_GRAY);
		host.setEditable(false);
		panel_1.add(host, "cell 1 1,alignx right");
		host.setText("localhost");
		host.setColumns(10);
		
		JLabel label_1 = new JLabel("כתובת בסיס הנתונים: ");
		panel_1.add(label_1, "cell 2 1");
		
		port = new JTextField();
		port.setBackground(Color.LIGHT_GRAY);
		port.setEditable(false);
		panel_1.add(port, "cell 1 2,alignx right");
		port.setText("3306");
		port.setColumns(10);
		
		JLabel lblport = new JLabel(": Port");
		panel_1.add(lblport, "cell 2 2");
		
		database = new JTextField();
		database.setBackground(Color.LIGHT_GRAY);
		database.setEditable(false);
		panel_1.add(database, "cell 1 3,alignx right");
		database.setText("hanitdb");
		database.setColumns(10);
		
		JLabel label_2 = new JLabel("שם בסיס הנתונים:");
		panel_1.add(label_2, "cell 2 3");
		
		username = new JTextField();
		username.setBackground(Color.LIGHT_GRAY);
		username.setEditable(false);
		username.setText("root");
		panel_1.add(username, "cell 1 4,alignx right");
		username.setColumns(10);
		
		JLabel label_3 = new JLabel("שם משתמש:");
		panel_1.add(label_3, "cell 2 4");
		
		password = new JTextField();
		password.setBackground(Color.LIGHT_GRAY);
		password.setEditable(false);
		panel_1.add(password, "cell 1 5,alignx right");
		password.setColumns(10);
		
		JLabel label_4 = new JLabel("סיסמא:");
		panel_1.add(label_4, "cell 2 5");
		
		closeServerButton = new JButton("סגירת השרת");
		closeServerButton.setIcon(new ImageIcon(ServerPanel.class.getResource("/graphics/exit.png")));
		closeServerButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) 
			{
				boolean result = Server.closeSrver();
				if (result)
				{
					runServerButton.setEnabled(true);
					closeServerButton.setEnabled(false);
				}
				else
				{
					JOptionPane.showMessageDialog(me,
        				    "שגיאה בסגירת השרת.","שגיאה",JOptionPane.ERROR_MESSAGE);
				}
				
			}
		});
		closeServerButton.setEnabled(false);
		panel.add(closeServerButton, "flowx,cell 1 3,alignx center");
		
		runServerButton = new JButton("הפעלת השרת");
		runServerButton.setIcon(new ImageIcon(ServerPanel.class.getResource("/graphics/server.png")));
		runServerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		runServerButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) 
			{
				runServer();
				
			}
		});
		panel.add(runServerButton, "cell 1 3");
		
		btnNewButton = new JButton("יצא לאקסל");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0)
			{
				exportToExcel();
			}
		});
		btnNewButton.setIcon(new ImageIcon(ServerPanel.class.getResource("/graphics/excel.png")));
		panel.add(btnNewButton, "cell 1 4,alignx center");
	}

	
	public void runServer()
	{
		//password.setText("han1than1t");
		(new Thread(new ServerThread(host.getText(), port.getText(), database.getText(), username.getText(), password.getText()))).start();
		runServerButton.setEnabled(false);
		closeServerButton.setEnabled(true);
	}


	/**
	 * Exports database to an excel file
	 */
	private void exportToExcel() 
	{
		ArrayList<Object> response = Server.sqlServer.exportToExcel();
		
		if (response.get(0).toString().equalsIgnoreCase("valid"))
		{
			JOptionPane.showMessageDialog(me,
    			    "בוצע ייצוא של בסיס הנתונים אל התיקיה" + "\n" + "c:\\Hanit","אישור",JOptionPane.INFORMATION_MESSAGE);
		}
		else
		{
			JOptionPane.showMessageDialog(me,
    			    "לא ניתן לייצא את בסיס הנתונים לאקסל, יש לוודא כי התיקיה" + "\n" + "c:\\Hanit" + "\n" + "קיימת וכי אין בה קבצי אקסל כלל.","שגיאה",JOptionPane.ERROR_MESSAGE);
		}
		
	}


/**
 * initializes data
 */
	public void setVariables() 
	{
		 try 
         {
				addressLabel.setText(InetAddress.getLocalHost().getHostAddress());
			}
         catch (UnknownHostException e) 
         {
         	addressLabel.setText("בעיה בחיבור לרשת");
				
				//e.printStackTrace();
		}
		 
		 
		 
		 
         
	}
	
	@Override
	public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    Color color1 = getBackground();
    Color color2 = color1.darker();
    int w = getWidth();
    int h = getHeight(); 
    GradientPaint gp = new GradientPaint(
            0, 0, color1,
            0, h, color2);

    g2d.setPaint(gp);
    g2d.fillRect(0, 0, w, h);
    }

}
